package com.wtwd.wtwddevice.service.impl;


import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wtwd.device.util.StringUtils;
import com.wtwd.wtwddevice.mapper.LastLocationDataMapper;
import com.wtwd.wtwddevice.model.entity.LastLocationData;
import com.wtwd.wtwddevice.service.IDeviceService;
import com.wtwd.wtwddevice.service.ILastLocationDataService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2024-05-15
 */
@Service
public class LastLocationDataServiceImpl extends ServiceImpl<LastLocationDataMapper, LastLocationData> implements ILastLocationDataService {

    @Resource
    private LastLocationDataMapper lastLocationDataMapper;

    @Resource
    private IDeviceService deviceService;

    @Override
    public boolean insertOrUpdate(LastLocationData lastLocationData) {
        if (lastLocationData == null) {
            return false;
        }
        lastLocationData.setCreateTime(new Date().getTime());
        lastLocationData.setUpdateTime(new Date().getTime());
        return lastLocationDataMapper.insertOrUpdate(lastLocationData) > 0 ? true : false;
    }

    @Override
    public LastLocationData getLastLocationDataByImei(String imei) {
        if (StringUtils.isNotEmpty(imei)) {
            return null;
        }
        Long deviceId = deviceService.getDeviceIdByImei(imei);
        if (deviceId == null) {
            return null;
        }
        return new LambdaQueryChainWrapper<>(lastLocationDataMapper)
                .eq(LastLocationData::getDeviceId, deviceId)
                .one();
    }
}
